/*
 * Created by JFormDesigner on Tue Jul 09 08:24:29 CST 2024
 */

package com.hongyan.dialog;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import javax.swing.*;
import javax.swing.border.*;

import com.hongyan.bean.Class;
import com.hongyan.bean.Test_sys;
import com.hongyan.config.MainFrameConfig;
import com.hongyan.dao.ClassDao;
import com.hongyan.dao.Test_sysDao;
import com.hongyan.view.admin.SwingAdminTestSysPanel;
import net.miginfocom.swing.*;
import org.jdesktop.swingx.JXDatePicker;

/**
 * @author admin
 */
public class SwingAdminTestSysDialog extends JDialog {
    private String AoU;

    public SwingAdminTestSysDialog(Window owner) {
        super(owner);
        initComponents();
        initData();
    }

    private void initData() {
        textField1.setVisible(false);
        label1.setVisible(false);
        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Test_sys sys = new Test_sys();
                sys.setSys_id(textField1.getText());
                sys.setSys_name(textField2.getText());
                sys.setDifficulty(textField3.getText());
                sys.setVersion(textField5.getText());
                sys.setLast_time(new Timestamp(JXDatePicker1.getDate().getTime()));
                int i = 0;
                if (AoU != "u") {
                    sys.setSys_id(MainFrameConfig.getSnowflakeUtils().getId());
                    i = Test_sysDao.insertSys(sys);
                } else {
                    i = Test_sysDao.updateSys(sys);
                }
                if (i > 0) {
                    JOptionPane.showMessageDialog(null, "添加成功");
                } else {
                    JOptionPane.showMessageDialog(null, "添加失败");
                }
                dispose();
            }
        });
    }

    public void updateSys(Test_sys sys, String a) {
        textField1.setText(sys.getSys_id());
        Test_sys uSys = Test_sysDao.findTest_sysById(sys.getSys_id());
        AoU = a;

        textField2.setText(uSys.getSys_name());
        textField3.setText(uSys.getDifficulty());
        textField5.setText(uSys.getVersion());
        JXDatePicker1.setDate(uSys.getLast_time());
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        label1 = new JLabel();
        textField1 = new JTextField();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        JXDatePicker1 = new JXDatePicker();
        label5 = new JLabel();
        textField2 = new JTextField();
        textField3 = new JTextField();
        textField5 = new JTextField();
        buttonBar = new JPanel();
        okButton = new JButton();
        cancelButton = new JButton();

        //======== this ========
        setTitle("\u6dfb\u52a0\u6d4b\u8bd5\u4f5c\u4e1a");
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(null);

                //---- label1 ----
                label1.setText("\u7cfb\u7edf\u7f16\u53f7\uff1a");
                contentPanel.add(label1);
                label1.setBounds(new Rectangle(new Point(40, 15), label1.getPreferredSize()));
                contentPanel.add(textField1);
                textField1.setBounds(115, 10, 185, textField1.getPreferredSize().height);

                //---- label2 ----
                label2.setText("\u7cfb\u7edf\u540d\u79f0\uff1a");
                contentPanel.add(label2);
                label2.setBounds(40, 55, 65, 17);

                //---- label3 ----
                label3.setText("\u96be\u5ea6\uff1a");
                contentPanel.add(label3);
                label3.setBounds(65, 95, 70, 17);

                //---- label4 ----
                label4.setText("\u4e0a\u4f20\u65f6\u95f4\uff1a");
                contentPanel.add(label4);
                label4.setBounds(40, 130, 70, 17);
                contentPanel.add(JXDatePicker1);
                JXDatePicker1.setBounds(115, 125, 185, JXDatePicker1.getPreferredSize().height);

                //---- label5 ----
                label5.setText("\u7248\u672c\u53f7\uff1a");
                contentPanel.add(label5);
                label5.setBounds(50, 165, 70, 17);
                contentPanel.add(textField2);
                textField2.setBounds(115, 50, 185, 23);
                contentPanel.add(textField3);
                textField3.setBounds(115, 90, 185, textField3.getPreferredSize().height);
                contentPanel.add(textField5);
                textField5.setBounds(115, 160, 185, 23);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < contentPanel.getComponentCount(); i++) {
                        Rectangle bounds = contentPanel.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = contentPanel.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    contentPanel.setMinimumSize(preferredSize);
                    contentPanel.setPreferredSize(preferredSize);
                }
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setLayout(new GridBagLayout());
                ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80};
                ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};

                //---- okButton ----
                okButton.setText("\u6dfb\u52a0");
                buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 5), 0, 0));

                //---- cancelButton ----
                cancelButton.setText("\u53d6\u6d88");
                buttonBar.add(cancelButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 0), 0, 0));
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel dialogPane;
    private JPanel contentPanel;
    private JLabel label1;
    private JTextField textField1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JXDatePicker JXDatePicker1;
    private JLabel label5;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField5;
    private JPanel buttonBar;
    private JButton okButton;
    private JButton cancelButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
